package cn.tac.framework.core.pojo;

import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.List;

/**
 * 定义前端表格控件使用的响应格式
 *
 * @author : tac
 * @since : 2017/7/14
 */
public class DataTableResponse<T> {
    @ApiModelProperty(value = "分页前的总条目数")
    private Long total;
    @ApiModelProperty(value = "分页后的数据")
    private List<T> rows;

    public Long getTotal() {
        if(total == null){
            return 0L;
        }
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List<T> getRows() {
        if(rows == null){
            rows = new ArrayList<>();
        }
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public void add(T row){
        getRows().add(row);
    }

    public void addAll(List<T> rows){
        getRows().addAll(rows);
    }

    public static DataTableResponse empty(){
        DataTableResponse o = new DataTableResponse();
        o.setTotal(0L);
        //noinspection unchecked
        o.setRows(new ArrayList<>());
        return o;
    }
}
